package com.spring.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spring.boot.entity.UserInfo;
import com.spring.boot.paramMap.UserMap;
import com.spring.boot.response.Result;
import com.spring.boot.service.SysUserRoleService;
import com.spring.boot.service.impl.UserServiceImpl;
import com.spring.boot.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.List;

@RestController
@PreAuthorize("hasAnyRole('userOption')")
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserServiceImpl userService;
    @Autowired
    private SysUserRoleService userRoleService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @PostMapping("/add")
    public Result<?> save(@RequestBody UserMap user){
        boolean result = userService.saveUser(user);//没有保存角色信息
        if(result){
            return Result.OK("新增成功",user);
        }
        else {
            return Result.error("新增失败！");
        }
    }
    @DeleteMapping("/delete/{id}")
    public Result<?> deleteUser(@PathVariable int id){
        userService.removeById(id);
        return Result.OK("删除成功!", id);
    }

    @PostMapping("/edit")
    public Result<?> updateUser(@RequestBody UserInfo user){
        String newPass = passwordEncoder.encode(user.getPassword());
        user.setPassword(newPass);
        userService.updateByUsername(user);
        return Result.OK("编辑成功");
    }



    @GetMapping("/page")
    public Result<?> listUser(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                              @RequestParam(name = "search", defaultValue = "") String name){
        Page<UserInfo> page = new Page<>(pageNum,pageSize);
        QueryWrapper<UserInfo> userQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            userQueryWrapper.like("username",name);
        }
        Page<UserInfo> pageList = userService.page(page, userQueryWrapper);
        return Result.OK(pageList);
    }

    @GetMapping("/getUser")
    public Result getUser(@RequestParam String username){
    UserInfo user = userService.getUserByUsername(username);
    return Result.OK("操作成功！",user);//todo 判断user没查到的情况
    }

    @GetMapping("/pageVo")
    public Result<?> listUserVo(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                              @RequestParam(name = "search", defaultValue = "") String name){
        Page<UserInfo> page = new Page<>(pageNum,pageSize);
        QueryWrapper<UserInfo> userQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            userQueryWrapper.like("username",name);
        }
        Page<UserVo> pageList = userService.pageVo(page, userQueryWrapper);
        return Result.OK(pageList);
    }

}
